
/*
 *  Copyright 2007 Marc Lanctot and Marc Gendron-Bellemare
 *
 *  This file is part of OpenMTG.
 *
 *  OpenMTG is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenMTG is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenMTG.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.util.*;

public class Helpers
{
  public static Random RNG = new Random();
  public static final double TOLERANCE = 0.0000001;

  public static int next_iid = Constants.IID_START;

  public static int nextIid(){
    next_iid++;
    return next_iid;
  }


  public static int abs(int x)
  {
    if (x < 0)
      x = (x*(-1));

    return x;
  }

  public static int min(int...nums)
  {
    int minimum = Integer.MAX_VALUE;

    for (int m : nums)
      if (m < minimum) minimum = m;

    return minimum;
  }

  public static int max(int...nums)
  {
    int maximum = Integer.MIN_VALUE;

    for (int m : nums)
      if (m > maximum) maximum = m;

    return maximum;
  }

  /** Returns true with probability p, false otherwise. */
  public static boolean prob(double p)
  {
    return (RNG.nextDouble() <= p);
  }

  public static boolean coinFlip()
  {
    return prob(0.5);
  }

  public static boolean doubleEquals(double a, double b)
  {
    return (Math.abs(a-b) < TOLERANCE);
  }

  public static String join(List list, String delimiter)
  {
    int i = 0;
    int size = list.size();
    //String s = "[";
    String s = "";

    for (Object o : list) {
      s += o.toString();
      if (i != (size-1))
        s += delimiter;

      i++;
    }

    //s+= "]";

    return s;
  }

  public static boolean vectorEquals(ArrayList<Integer> v1,
                                     ArrayList<Integer> v2)
  {
    if (v1.size() != v2.size())
      return false;

    for (int i = 0; i < v1.size(); i++)
    {
      double a = v1.get(i);
      double b = v2.get(i);

      if (a != b)
        return false;
    }

    return true;
  }

  public static void printVars(Object...objs)
  {
    for (Object o : objs)
      System.out.print(o.toString() + " ");

    System.out.println("");
  }

  public static String getColor(Card c)
  {
    if (c.isLand())
      return Constants.CLR_END;
    
    String clrs = c.getColors();
    
    if (clrs.length() == 1)
    {
      if (clrs.equals("W"))
        return Constants.CLR_BRWHITE;
      else if (clrs.equals("U"))
        return Constants.CLR_BRBLUE;
      else if (clrs.equals("B"))
        return Constants.CLR_DRGREY;
      else if (clrs.equals("G"))
        return Constants.CLR_BRGREEN;
      else if (clrs.equals("R"))
        return Constants.CLR_BRRED;      
    }
    else if (clrs.length() == 2)
    {
      return Constants.CLR_BRYELLOW;
    }
    
    return Constants.CLR_END;
  }
  
  public static String colorizeManaPool(String s)
  {
    if (s.equals(""))
      return "";
    
    String res = "";
    
    for (int i = 0; i < s.length(); i++)
    {
      if (s.charAt(i) == 'W')
        res += (Constants.CLR_BRWHITE + "W");
      else if (s.charAt(i) == 'U')
        res += (Constants.CLR_BRBLUE + "U");
      else if (s.charAt(i) == 'G')
        res += (Constants.CLR_BRGREEN + "G");
      else if (s.charAt(i) == 'R')
        res += (Constants.CLR_BRRED + "R");
      else if (s.charAt(i) == 'B')
        res += (Constants.CLR_DRGREY + "B");
    }
      
    res += Constants.CLR_END;
    
    return res;
  }
  
  public static void sleep(int ms)
  {
    try 
    {
      Thread.sleep(ms); 
    }
    catch(Exception e)
    {
      throw new RuntimeException(e); 
    }
  }
  
  public static void error(String s)
  {
    throw new RuntimeException(s); 
  }
}

